<?php
/**
 * (jobs) 回写备货设定日期(批量操作)
 */
namespace App\Jobs\Stocking;

use App\Models\Stocking\StockingSku;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SetDates implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $stockingSetDates;

    /**
     * SetDates constructor.
     * @param $stockingSetDates
     */
    public function __construct($stockingSetDates)
    {
        $this->stockingSetDates = $stockingSetDates;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->stockingSetDates);
    }

    private function exeFillData($stockingSetDates){
        foreach ($stockingSetDates as $stockingSetDate){
            $classifyId = $stockingSetDate->classify_id;
            $ss = StockingSku::query()
                ->where('area_id',$stockingSetDate->area_id)
                ->whereHas('sku',function ($query)use ($classifyId){
                    $query->where('classify_id',$classifyId);
                })->get();
            foreach ($ss as $_ss){
                $_ss->set_date = $stockingSetDate->set_date;
                $_ss->save();

                $_ss->over_stock = StockingSku::getOverStock($_ss); //积压库存
                $_ss->over_stock_value = StockingSku::getOverStockValue($_ss->over_stock,$_ss->unit_price); //积压货值
                $_ss->save();
            }
        }
    }
}
